<template>
  <div class="seat-table">
    <el-tag
      :key="tag"
      v-for="tag in subject"
      closable
      :disable-transitions="false"
      @close="handleClose(tag)">
      {{tag}}
    </el-tag>
    <el-input
      class="input-new-tag"
      v-if="inputVisible"
      v-model="inputValue"
      ref="saveTagInput"
      size="small"
      @keyup.enter.native="handleInputConfirm"
      @blur="handleInputConfirm"
    >
    </el-input>
    <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新课程</el-button>
    <div>
      <el-button type="primary" style="margin-top: 20px;" @click="next">下一步</el-button>
    </div>
  </div>
</template>

<script>
import storage from '@/utils/storage'

export default {
  name: 'edit-subject',
  data () {
    return {
      subject: storage.getItem('subject') || ['语文', '数学', '英语'],
      inputVisible: false,
      inputValue: ''
    }
  },
  methods: {
    handleClose (tag) {
      this.subject.splice(this.subject.indexOf(tag), 1)
    },

    showInput () {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm () {
      const inputValue = this.inputValue
      if (inputValue) {
        this.subject.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },

    next () {
      storage.setItem('subject', this.subject)
      this.$parent.$parent.initActive = 3
    }
  }
}
</script>

<style lang="scss" scoped>
.seat-table {
  margin: 40px 80px;
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
}
</style>
